<template>
	<!-- 通用版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('public.materialCode')" />
		<!-- 表格数据 -->
		<zy-table :headList="headList" :dataList="dataList" @editInput="editInput" :operationList="operationList"
			border>
		</zy-table>
		<!-- 底部按钮 -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')" @rightBtn="submit"
			@leftBtn="resetBtn" :leftDisabled="disabled" :rightDisabled="disabled">
		</bottomBtn>
		<!-- 提交二次确认 -->
		<zy-popup ref="popup" :title="$t('homepage.WarehouseManage.cancelConfirm')" type="dialog" @confirm="submitBtn">
			{{$t('public.isSure')}}{{$t('homepage.WarehouseManage.cancelConfirm')}}{{$t('public.question')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		getStorehouseDetailByCode,
		saveBack
	} from "@/api/homepage.js"
	export default {
		data() {
			return {
				//表格表头
				headList: [{
					name: this.$t('public.materialCode'),
					key: 'materialCode',
				}, {
					name: this.$t('public.batchNo'),
					key: 'batchNo',
				}, {
					name: this.$t('homepage.WarehouseManage.cancelNum'),
					key: 'num',
					edit: true,
					type: 'number'
				}],
				dataList: [], //表格数据
				//表格操作列
				operationList: [{
					kind: "button",
					type: "warn",
					handle: this.delBtn,
					label: this.$t('public.del'),
				}],
				disabled: true,
			}
		},
		watch: {
			dataList: {
				handler(n, o) {
					if (n.length > 0) {
						this.disabled = false
					} else {
						this.disabled = true
					}
				},
				immediate: true,
				deep: true
			}
		},
		onLoad() {
			// 移除监听“添加扫描”弹窗事件
			uni.$off('scancodepopdate')
			//PDA扫描主页指示单号
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList(e) {
				getStorehouseDetailByCode({
					code: e,
				}).then((res) => {
					if (res.code == 0) {
						if (res.data.status == 1 || res.data.status == 2) {
							if (this.dataList.length == 0) {
								let obj = {}
								obj.materialCode = res.data.materialCode
								obj.batchNo = res.data.batchNo
								obj.id = res.data.id
								obj.num = 1
								this.dataList.push(obj)
							} else {
								if (this.dataList.findIndex(item => item.barcode === e) !== -1) {
									uni.showToast({
										title: this.$t('public.searchRepeat'),
										icon: "none",
										duration: 3000
									})
								} else {
									let obj = {}
									obj.materialCode = res.data.materialCode
									obj.batchNo = res.data.batchNo
									obj.id = res.data.id
									obj.num = 1
									this.dataList.push(obj)
								}
							}
						} else {
							uni.showToast({
								title: this.$t('homepage.WarehouseManage.thiscode') + res.data.statusText,
								icon: "none",
								duration: 3000
							})
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//编辑出库数量
			editInput(val) {
				if (Number(val.item.num) <= 0) {
					setTimeout(() => {
						uni.showToast({
							title: this.$t('homepage.WarehouseManage.numThanZaro'),
							icon: "none",
							duration: 3000
						})
						this.dataList[val.index].num = 1
					}, 500)
				}
			},
			// 删除数据事件
			delBtn(item, index) {
				this.dataList.splice(index, 1);
			},
			//重置按钮
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/StorageManagement/Cancel/index"
				})
			},
			//退订二次确认按钮事件
			submit() {
				this.$refs.popup.open()
			},
			//确认出库
			submitBtn() {
				this.$refs.popup.close()
				let obj = ""
				for (var i = 0; i < this.dataList.length; i++) {
					obj += this.dataList[i].id + ":" + this.dataList[i].num + ','
				}
				saveBack({
					dataArray: obj,
				}).then((res) => {
					console.log(res)
					if (res.code == 0) {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
						this.resetBtn()
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
	.formItem {
		width: calc(100% - 40rpx);
		padding: 20rpx;
		display: flex;
		border: 1px solid #e9e9e9;
		border-radius: 10rpx;
		background-color: #f5f5f5;
	}

	.formInput {
		margin: auto 0;
		width: 100%;
		border: none;
		outline: none;
		font-size: 32rpx;
		z-index: 999999;
	}

	.iconfont {
		margin: auto 0 auto 10rpx;
		font-size: 28rpx;
		color: #999999;
	}
</style>